#include "CGrenade.h"
#include "..\CCyberSpaceState.h"
#include "..\COptionsState.h"
#include "..\CGame.h"

CGrenade::CGrenade(void)
{
	SetWidth(32);
	SetHeight(32);

	m_fExpTimer = 0.0f;
	m_fDestroyTimer = 0.0f;

	m_bIsExploding = false; // The grenade will not start at the beginning

	// Load sounds
	m_nExExplodeID = CSGD_FModManager::GetInstance()->LoadSound("Resource/Sound/SGP_ExGrenadeExplode.wav");
	m_nExGrenID = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/SGP_ExGrenade.png");

	// Set volume
	CSGD_FModManager::GetInstance()->SetVolume(m_nExExplodeID, COptionsState::GetInstance()->GetSFXVolume());
}

CGrenade::~CGrenade(void)
{

}

void CGrenade::Update(float fElapsedTime)
{
	CBase::Update(fElapsedTime);

	if(GetVelX() > 0)
	{
		SetVelX(GetVelX() - 3);
	}
	else if(GetVelX() < 0)
	{
		SetVelX(GetVelX() + 3);
	}

	if(GetVelY() > 0)
	{
		SetVelY(GetVelY() - 3);
	}
	else if(GetVelY() < 0)
	{
		SetVelY(GetVelY() + 3);
	}

	// Update the explosion radius
	m_rExRadius.left = (LONG)GetPosX() - 30;
	m_rExRadius.top = (LONG)GetPosY() - 30;
	m_rExRadius.right = (LONG)(GetPosX() + 30) + GetWidth();
	m_rExRadius.bottom = (LONG)(GetPosY() + 30) + GetHeight();

	// Check if its time to explode
	m_fExpTimer += fElapsedTime;
	if(m_fExpTimer > 3.0f)
	{
		m_bIsExploding = true;
		if(!CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nExExplodeID))
			CSGD_FModManager::GetInstance()->PlaySoundA(m_nExExplodeID);
	}

	if(m_bIsExploding)
	{
		m_fDestroyTimer += fElapsedTime;
		if(m_fDestroyTimer > 1.0f)
		{
			CSGP_MessageSystem::GetInstance()->SendMessage(new CDestroyGrenadeMessage(this));
		}

	}

	if(GetPosX() < 0 || GetPosX() > CGame::GetInstance()->GetScreenWidth()
		|| GetPosY() < 0 || GetPosY() > CGame::GetInstance()->GetScreenHeight() - GetHeight())
	{
		// Destroy the grenade
		//CSGP_MessageSystem::GetInstance()->SendMessage(new CDestroyGrenadeMessage(this));
	}
}

void CGrenade::Render(void)
{
	RECT drawRect = GetCollisionRect();
	drawRect.top -= CCamera::GetInstance()->GetOffsetY();
	drawRect.left -= CCamera::GetInstance()->GetOffsetX();
	drawRect.right -= CCamera::GetInstance()->GetOffsetX();
	drawRect.bottom -= CCamera::GetInstance()->GetOffsetY();

	// Draw grenade
	CSGD_TextureManager::GetInstance()->Draw(m_nExGrenID, drawRect.left, drawRect.top, 1.0f, 1.0f);

	// Draw collision box
	//CSGD_Direct3D::GetInstance()->DrawRect(drawRect, 0, 255, 0);

	// Draw the explosion rectangle
	/*if(m_bIsExploding)
	{
		CSGD_Direct3D::GetInstance()->DrawRect(m_rExRadius, 255, 0, 0);
	}*/
}

RECT CGrenade::GetCollisionRect(void)
{
	RECT rIntersect;
	rIntersect.left = long(GetPosX());
	rIntersect.top = long(GetPosY());
	rIntersect.right = long(rIntersect.left + GetWidth());
	rIntersect.bottom = long(rIntersect.top + GetHeight());

	return rIntersect;
}

bool CGrenade::CheckCollision(IBaseInterface* pBase)
{

	return false;
}